using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

namespace DbService
{
   /// <summary>
   /// Summary description for Registreren
   /// </summary>
   [WebService(Namespace = "http://localhost")]
   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   [ToolboxItem(false)]
   public class Registreren : System.Web.Services.WebService
   {

       [WebMethod]
       public bool RegisterUser1(Klant k, string password)
       {
           return RegisterUser(k.Email, password, k.Firstname, k.Lastname, k.Name_addition, k.Address, "" + k.House_number, k.House_number_add, k.City, k.Postal_code, "" + k.Member_number);
       }


      [WebMethod]
      public bool RegisterUser(string email, string password, string firstname, string lastname, string name_addition, string address, 
         string house_number, string house_number_add, string city, string postal_code, string member_number)
      {
         SqlConnection sqlConn = null;

         try
         {
            sqlConn = new SqlConnection(Global.GetConnectionString());
            sqlConn.Open();

            SqlCommand sqlcmdValidateLogin = new SqlCommand("sp_createLogin", sqlConn);
            sqlcmdValidateLogin.CommandType = CommandType.StoredProcedure;

            sqlcmdValidateLogin.Parameters.Add(new SqlParameter("email", email));
            sqlcmdValidateLogin.Parameters.Add(new SqlParameter("password", password));
            sqlcmdValidateLogin.Parameters.Add(new SqlParameter("firstname", firstname));
            sqlcmdValidateLogin.Parameters.Add(new SqlParameter("lastname", lastname));
            sqlcmdValidateLogin.Parameters.Add(new SqlParameter("name_addition", name_addition));
            sqlcmdValidateLogin.Parameters.Add(new SqlParameter("adress", address));
            sqlcmdValidateLogin.Parameters.Add(new SqlParameter("house_number", house_number));
            sqlcmdValidateLogin.Parameters.Add(new SqlParameter("house_number_add", house_number_add));
            sqlcmdValidateLogin.Parameters.Add(new SqlParameter("city", city));
            sqlcmdValidateLogin.Parameters.Add(new SqlParameter("postal_code", postal_code));
            sqlcmdValidateLogin.Parameters.Add(new SqlParameter("member_number", member_number));

            sqlcmdValidateLogin.ExecuteNonQuery();

            ERP.Klant klant = new ERP.Klant();
            klant.achternaam = lastname;
            klant.adres = address;
            klant.gebruikersnaam = email;
            klant.mailadres = email;
            klant.postcode = postal_code;
            klant.voornaam = firstname;
            klant.wachtwoord = "nopass"; // password set to no pass because we dont
                                         // want the application to send the hashed
                                         // password everywhere to every other service for security reasons;

            klant.woonplaats = city;
            ERP.transactie transactie = new ERP.transactie();
            string returns = transactie.registreerKlant(klant);            
           }
         catch (SqlException ex)
         {            
            Global.WriteDebugLine(ex.ToString());
         }
         finally
         {
            if (null != sqlConn)
            {
               sqlConn.Close();
            }
         }
         return false;
      }
   }
}
